package com.yly.doctorapp.ui.home.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.yly.doctorapp.R;
import com.yly.doctorapp.base.BaseFragment;
import com.yly.doctorapp.base.DoctorAppApplication;
import com.yly.doctorapp.constant.Configution;
import com.yly.doctorapp.ui.order.OrderListActivity;
import com.yly.doctorapp.ui.usercenter.ChangePhoneActivity;
import com.yly.doctorapp.ui.usercenter.ForgetPasswordActivity;
import com.yly.doctorapp.ui.usercenter.LoginActivity;
import com.yly.doctorapp.widget.GlideCircleTransform;
import com.zhcx.zhcxlibrary.utils.AppManager;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;
import com.zhcx.zhcxlibrary.utils.SPUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class MineFragment extends BaseFragment {
    @BindView(R.id.iv_minehead)
    ImageView ivMinehead;
    @BindView(R.id.tv_doctorname)
    TextView tvDoctorname;
    @BindView(R.id.tv_doctortitle)
    TextView tvDoctortitle;
    @BindView(R.id.tv_doctortype)
    TextView tvDoctortype;
    @BindView(R.id.tv_begoodat)
    TextView tvBegoodat;
    @BindView(R.id.tv_brief_introduction)
    TextView tvBriefIntroduction;
    @BindView(R.id.ll_order)
    LinearLayout llOrder;
    @BindView(R.id.ll_changepassword)
    LinearLayout llChangepassword;
    @BindView(R.id.ll_changephone)
    LinearLayout llChangephone;
    @BindView(R.id.tv_logout)
    TextView tvLogout;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_ContactUs_content)
    LinearLayout llContactUsContent;
    @BindView(R.id.ll_ContactUs)
    LinearLayout llContactUs;
    @BindView(R.id.image_updown)
    ImageView image_updown;
    @BindView(R.id.tv_tellphone)
    TextView mTellPhone;
    private SPUtils mSpurils;
    private boolean isVis = false;


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mSpurils = new SPUtils(getActivity());
        if (null != DoctorAppApplication.mUserBean) {
            tvDoctorname.setText(DoctorAppApplication.mUserBean.getDoctorname());
            tvDoctortitle.setText(DoctorAppApplication.mUserBean.getPosttitle());
            tvDoctortype.setText(DoctorAppApplication.mUserBean.getHospital());
            tvBegoodat.setText("擅长:" + DoctorAppApplication.mUserBean.getGoodat());
            if (StringUtils.isEmpty(DoctorAppApplication.mUserBean.getDoctorintro())) {
                tvBriefIntroduction.setText("");
            } else {
                tvBriefIntroduction.setText("简介:" + DoctorAppApplication.mUserBean.getDoctorintro());
            }
            if (StringUtils.isEmpty(DoctorAppApplication.mUserBean.getTelephone())) {
                tvPhone.setText("");
            } else {
                if (DoctorAppApplication.mUserBean.getTelephone().length() == 11) {
                    String phone = DoctorAppApplication.mUserBean.getTelephone();
                    tvPhone.setText(phone.substring(0, 3) + " " + phone.substring(3, 7) + " " + phone.substring(7, 11));
                } else {
                    tvPhone.setText(DoctorAppApplication.mUserBean.getTelephone());
                }
            }
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.minehead)
                    .error(R.mipmap.minehead)
                    .priority(Priority.HIGH)
                    .transform(new GlideCircleTransform());
            //加载圆形图片
            Glide.with(getActivity()).load(DoctorAppApplication.mUserBean.getHeadImg()).apply(options).into(ivMinehead);
        }
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.ll_order, R.id.ll_changepassword, R.id.ll_changephone, R.id.tv_logout, R.id.ll_ContactUs, R.id.tv_tellphone})
    public void onViewClicked(View view) {
        Intent mIntent = null;
        switch (view.getId()) {
            case R.id.ll_order:
                mIntent = new Intent(getActivity(), OrderListActivity.class);
                startActivity(mIntent);
                break;
            case R.id.ll_changepassword:
                if (null != DoctorAppApplication.mUserBean) {
                    mIntent = new Intent(getActivity(), ForgetPasswordActivity.class);
                    mIntent.putExtra("phone", DoctorAppApplication.mUserBean.getTelephone());
                    mIntent.putExtra("type", "1");//
                    startActivity(mIntent);
                }
                break;
            case R.id.ll_changephone:
                if (null != DoctorAppApplication.mUserBean) {
                    mIntent = new Intent(getActivity(), ChangePhoneActivity.class);
                    mIntent.putExtra("phone", DoctorAppApplication.mUserBean.getTelephone());
                    startActivity(mIntent);
                }
                break;
            case R.id.tv_logout:
                showLogoutDialog();
                break;
            case R.id.ll_ContactUs:
                if (!isVis) {
                    llContactUsContent.setVisibility(View.VISIBLE);
                    image_updown.setImageResource(R.mipmap.icon_down);
                    isVis = true;
                } else {
                    llContactUsContent.setVisibility(View.GONE);
                    image_updown.setImageResource(R.mipmap.icon_up);
                    isVis = false;
                }
                break;
            case R.id.tv_tellphone:
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + mTellPhone.getText().toString().trim().replace("-", ""));
                intent.setData(data);
                startActivity(intent);
                break;
        }
    }


    private void showLogoutDialog() {
        final Dialog dialog = new Dialog(getActivity(), R.style.buscarddialog);
        View view = View.inflate(getActivity(), R.layout.layout_logoutdialog, null);
        dialog.setContentView(view);
        TextView tv_cancel = view.findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView tv_sure = view.findViewById(R.id.tv_sure);
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent mIntent = new Intent(getActivity(), LoginActivity.class);
                startActivity(mIntent);
                mSpurils.putString(Configution.USERPASSWORD, "");
                AppManager.getAppManager().finishAllActivity();
            }
        });
        dialog.show();
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (DeviceUtils.deviceWidth(mContext) * 0.5);
        lp.height = (int) (DeviceUtils.deviceWidth(mContext) * 0.3);
        dialogWindow.setAttributes(lp);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
